package com.lecture.javamode.oberver;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

public class UserController {

    private EventBus eventBus;
    private final static int DEFAULT_EVENTBUS_THREAD_POOL_SIZE = 20;

    public UserController() {
        //eventBus = new EventBus(); //同步阻塞
        // 异步阻塞
        eventBus = new AsyncEventBus(Executors.newFixedThreadPool(DEFAULT_EVENTBUS_THREAD_POOL_SIZE));
    }

    public void setRegObservers(List<Object> observers) {
        for (Object observer : observers) {
            eventBus.register(observer);
        }
    }

    public void register() {
        //注册用户
        long userId = 1L;

        // 通知相关观察者,执行对应的方法
        eventBus.post(userId);
    }

    public static void main(String[] args) {
        RegNotificationObserver regNotificationObserver = new RegNotificationObserver();
        RegPromotionObserver regPromotionObserver = new RegPromotionObserver();
        List<Object> observers = new ArrayList<>();
        observers.add(regPromotionObserver);
        observers.add(regNotificationObserver);
        UserController userController = new UserController();
        userController.setRegObservers(observers);

        userController.register();
    }
}
